#include<vector>
#include<string>

using namespace std;


class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) return s;

        vector<string> rows(numRows);
        int cycle = 2 * numRows - 2; 

        for (int i = 0; i < numRows; ++i) {
            if (i == 0 || i == numRows - 1) {
                for (int j = i; j < s.size(); j += cycle) {
                    rows[i].push_back(s[j]);
                }
            }
            else {
                int step = 0;
                for (int j = i; j < s.size(); j += cycle) {
                    rows[i].push_back(s[j]);
                    int nextPos = (step + 1) * cycle - i;
                    if (nextPos < s.size()) {
                        rows[i].push_back(s[nextPos]);
                    }
                    step++;
                }
            }
        }

        string result;
        for (const string& row : rows) {
            result += row;
        }

        return result;
    }
};

int main()
{
    Solution s;
    s.convert("PAYPALISHIRING", 3);
    return 0;
}